package com.boc.langxiang.presenter.adm;


import com.boc.langxiang.contract.adm.AdmSearchContract;
import com.boc.langxiang.entity.request.AdmListRequest;
import com.boc.langxiang.entity.request.AdmSearchRequest;
import com.boc.langxiang.entity.response.AdmEntity;
import com.boc.langxiang.entity.response.AdmListContent;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CreateTime: 2016-06-14上午10:40
 * Author: wjkjinke00@126.com
 * Description:
 */
public class AdmSearchPresenter implements AdmSearchContract.Presenter {

    private final ApiService mService;
    private AdmSearchContract.View mView;

    public AdmSearchPresenter(AdmSearchContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        mView = view;
    }


    @Override
    public void admSearch(AdmSearchRequest request) {
        mView.showLoading();
        mService.admSearch(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<AdmListContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<AdmListContent> response) {
                        AdmListContent content = response.getContent();
                        if (content != null) {
                            List<AdmEntity> res = content.getRes();
                            mView.admSearchSuccess(res);
                        }
                    }
                });
    }

    @Override
    public void hotWord() {
        mView.showHotWordLoading();
        mService.wordList(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ArrayList<String>>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ArrayList<String>> response) {
                        ArrayList<String> content = response.getContent();
                            mView.hotWordSuccess(content);
                    }
                });
    }
}
